package cn.wolfcode.rbac.web.controller.report_statistics;

import cn.wolfcode.rbac.query.CustomerReportQuery;
import cn.wolfcode.rbac.query.QueryObject;
import cn.wolfcode.rbac.service.report_statistics.ICustomerReportService;
import cn.wolfcode.rbac.util.MessageUtil;
import cn.wolfcode.rbac.util.PageResult;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping({"/customerReport"})
public class CustomerReportController {
    @Autowired
    private ICustomerReportService customerReportService;



    public CustomerReportController() {
    }

//    @RequestMapping({"/list"})
//    public String list(Model model, @ModelAttribute("qo") CustomerReportQuery qo) {
//        model.addAttribute("result", this.customerReportService.selectCustomerReport(qo));
//        return "customerReport/list";
//    }
    private CustomerReportQuery qo=new CustomerReportQuery();

//    @RequestMapping({"/list"})
//    public String list(Model model) {
//       // model.addAttribute("result", this.customerReportService.selectCustomerReport(this.qo));
//
//        System.out.println();
//        model.addAttribute("qo",this.qo);
//        return "customerReport/list";
//    }

    @RequestMapping("/list")
    public String list(Model model, QueryObject queryObject){

        // 查询出数据
        PageResult result = customerReportService.query(this.qo);
//        System.out.println(departments);
        // 回显数据
        model.addAttribute("result",result);
        model.addAttribute("qo",this.qo);

        return "/customerReport/list";
    }





    @RequestMapping({"/querylist"})
    @ResponseBody
    public String querylist(Model model, @RequestBody JSONObject param)throws ParseException {//@RequestBody JSONObject param
        String startStr = param.getString("beginDate");
        String endStr =param.getString("endDate");
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
        Date newStartDate ;
        Date newEndDate ;
        java.sql.Date resultStDate ;
        java.sql.Date resultEnDate ;
        try{
             newStartDate =sdf2.parse(startStr);
            newEndDate =sdf2.parse(endStr);
            resultStDate = new java.sql.Date(newStartDate.getTime());
            resultEnDate = new java.sql.Date(newEndDate.getTime());
            this.qo.setBeginDate(resultStDate);
            this.qo.setEndDate(resultEnDate);
        }catch (Exception e){
            e.printStackTrace();
        }
       // System.out.println(resultStDate);

       // System.out.println("------->"+startStr);

        this.qo.setKeyword(param.getString("keyword"));
        this.qo.setGroupType(param.getString("groupType"));




        PageResult resultlist=this.customerReportService.query(this.qo);
        List<Map> list=resultlist.getList();

        for(Object i:list){
            System.out.println(i);
        }
      model.addAttribute("result",resultlist );
//        List<Map> list=this.customerReportService.listAll(this.qo);
        model.addAttribute("qo",this.qo);


//        return "redirect:/customerReport/list";

        return JSON.toJSON(list).toString();
    }





    @RequestMapping("/listByBar")
 //   @ResponseBody
    public String listByBar(Model model, @ModelAttribute("qo") CustomerReportQuery qo) {

        List<Map> list = this.customerReportService.listAll(qo);
        ArrayList xList = new ArrayList();
        ArrayList yList = new ArrayList();
        Iterator var6 = list.iterator();

        while(var6.hasNext()) {
            Map map = (Map)var6.next();
            xList.add(map.get("groupType"));
            yList.add(map.get("number"));
        //    System.out.println("--->"+map.get("groupType"));
        //    System.out.println("--->"+map.get("number"));
        }

        System.out.println(JSON.toJSONString(xList));
        model.addAttribute("xList", JSON.toJSONString(xList));
        model.addAttribute("yList", JSON.toJSONString(yList));
        model.addAttribute("groupTypeName", MessageUtil.changMsg(qo));
        return "/customerReport/listByBar";
    }




    @RequestMapping("/listByPie")
    public String listByPie(Model model, @ModelAttribute("qo") CustomerReportQuery qo) {
        List<Map> list = this.customerReportService.listAll(qo);
        ArrayList xList = new ArrayList();
        ArrayList yList = new ArrayList();
        Iterator var6 = list.iterator();

        while(var6.hasNext()) {
            Map map = (Map)var6.next();
            xList.add(map.get("groupType"));
            HashMap<String, Object> temp = new HashMap();
            temp.put("name", map.get("groupType"));
            temp.put("value", map.get("number"));
            yList.add(temp);
        }

        model.addAttribute("xList", JSON.toJSONString(xList));
        model.addAttribute("yList", JSON.toJSONString(yList));
        model.addAttribute("groupTypeName", MessageUtil.changMsg(qo));
        return "customerReport/listByPie";
    }
}
